package com.gec.controller;

import com.gec.model.system.SysMovie;
import com.gec.service.SysMovieService;
import com.gec.system.common.Result;
import com.gec.system.util.OssTemplate;
import com.gec.system.util.VodTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Api(tags = "视频上传控制器")
@RestController
@RequestMapping("/admin/system/upload")
@CrossOrigin  // 解决跨域问题
public class UploadController {
    @Autowired
    private OssTemplate ossTemplate;
    @Autowired
    private SysMovieService sysMovieService;
    @Autowired
    private VodTemplate vodTemplate;

    @ApiOperation("上传图片")
    @RequestMapping("/uploadImage")
    public Result uoloadImag(MultipartFile uploadImage) throws IOException {
        //获取文件名和输入流
        String upload = this.ossTemplate.upload(uploadImage.getOriginalFilename(), uploadImage.getInputStream());
        System.out.println("upload = " + upload);
        return Result.ok(upload);
    }

    @ApiOperation("上传视频")
    @PostMapping("/uploadVideo")
    public Result uploadVideo(MultipartFile uploadVideo) throws IOException {
        String videoId = this.vodTemplate.uploadVideo(uploadVideo.getOriginalFilename(), uploadVideo.getInputStream());
        System.out.println("videoId = " + videoId);
        return Result.ok(videoId);
    }

    @ApiOperation("根据id获取播放凭证")
    @GetMapping("/getPlayAuth/{id}")
    public Result getPlayAuth(@PathVariable Long id) throws Exception {
        // 1.根据id获取movie
        SysMovie sysMovie = this.sysMovieService.getById(id);
        System.out.println("sysMovie为" + sysMovie);
        //2.根据movie获取播放id
        String playId = sysMovie.getPlayId();
        //3.获取 封面
        String image = sysMovie.getImage();
        //4.根据播放id获取auth
        String playAuth = this.vodTemplate.getVideoPlayAuth(playId).getPlayAuth();
        Map<String, Object> map = new HashMap<>();
        map.put("image", image);
        map.put("playId", playId);
        map.put("playAuth", playAuth);

        return Result.ok(map);
    }
}
